package cn.app.web.controller;

import java.io.File;
import java.io.UnsupportedEncodingException;

import javax.servlet.http.HttpSession;

import org.apache.commons.io.FileUtils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.app.entity.DevUser;
import cn.app.service.DevUserService;
import cn.app.utils.MD5Utils;


@Controller
@RequestMapping("/devUser")
public class DevUserController {
	@Autowired
	private DevUserService devUserService;
		
		@RequestMapping("/disLogin")
		public String disLogin(){
			
			return "devLogin";
		}
		@RequestMapping("login")
		public String login(HttpSession session,Model model,
							@RequestParam(value="username",required=false)String devcode,
							@RequestParam(value="password",required=false)String devpassword)throws Exception{
			devpassword=MD5Utils.md5(devpassword);
			SecurityUtils.getSubject().login(new UsernamePasswordToken(devcode,devpassword));
			
//			SecurityUtils.getSubject().getSession().setAttribute("devUser", devUser);
//			Subject subject = SecurityUtils.getSubject();
			
			
			
			
			//跳转主页
			return "redirect:/devApp/disMain";
			/*DevUser devUser=devUserService.login(devcode,devpassword);
			if(devUser!=null){
				//登陆跳转
				session.setAttribute("devUser", devUser); 
				//跳转主页
				return "redirect:/devApp/disMain";
			}else{
				//登陆失败
				model.addAttribute("loginInfo", "用户名或者密码错误。。。");
				return "devLogin";
			}*/
			
		}
		
		@RequestMapping("/disRegister")
		public String disRegister(){
			return "register";
		}
		
		//json校验用户名
		@RequestMapping("/checkUsername")
		public @ResponseBody DevUser checkUsername(@RequestBody DevUser devUser){
			DevUser user2 = devUserService.findDevUserByDevCode(devUser);
			return user2;
		}
		//注册功能
		@RequestMapping("/register")
		public String register(String username, String password, Model model,String phoneCode,HttpSession session)throws Exception {
			//判断用户输入的手机验证码是否正确
			String smsCode=(String)session.getAttribute("smsCode");
			
			if(smsCode.equals(phoneCode)){
			// MD5加密
			password = MD5Utils.md5(password);
			// 调用service
			int count = devUserService.save(username, password);
			if (count > 0) {
				// 注册成功
				return "user/login";
			} else {
				// 注册失败
				model.addAttribute("registerInfo", "系统正在更新，请与管理员联系。。。。");
				return "user/register";
			}
			}else{
				//验证码输入错误
				model.addAttribute("registerInfo", "验证码输入错误");
				return "user/register";
			}
		}
		
/*	@RequestMapping("/download")
	public ResponseEntity<byte[]> download() throws Exception{
		
		String path="";
		File file=new File("下载照片的绝对路径");
		HttpHeaders headers=new org.springframework.http.HttpHeaders();
		String fileName=new String("照片名".getBytes("UTF-8"),"iso-8859-1");//解决中文乱码问题
		headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
		headers.setContentDispositionFormData("attachment", fileName);
		return new ResponseEntity<>(FileUtils.readFileToByteArray(file),headers,HttpStatus.CREATED);
		
	}*/
		/*//退出登录
		@RequestMapping("logout")
		public String logout(HttpSession session){
			//将session域中的用户信息清除掉
			session.removeAttribute("devUser");
			//跳转到登录界面
			
			
			return "devLogin";
		}*/
}
